package com.yb.oldlearning;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 优先级
 * 可移植的三种优先级别：MAX_PRIORITY,NORM_PRIORITY,MIN_PRIORITY
 * @author Administrator
 */
public class SimplePriorities implements Runnable{
	private int countDown = 5;
	private volatile double d;//volatile类型的变量意义是所有线程在访问到这个变量时都是访问的这个变量最新的值
	private int priority;
	public SimplePriorities(int priority){
		this.priority = priority;
	}
	public String toString(){
		return Thread.currentThread() + ": " + countDown;
	}
	public void run(){
		Thread.currentThread().setPriority(priority);
		while(true){
			for(int i =1;i<100000;i++){
				d += (Math.PI + Math.E)/(double)i;
				if (i%1000 == 0) {
					Thread.yield();
				}
			}
			System.out.println(this);
			if(--countDown == 0) return;
		}
	}
	
	public static void main(String[] args) {
		ExecutorService exec = Executors.newCachedThreadPool();
		for(int i=0;i<3;i++){
			exec.execute(new SimplePriorities(Thread.MIN_PRIORITY));
			exec.execute(new SimplePriorities(Thread.MAX_PRIORITY));
		}
	}
}
